package zsw.clone;

import java.io.ByteArrayOutputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;

import org.junit.Test;

public class TestClone {

	public static void main(String[] args) throws CloneNotSupportedException {
		Cat s=new Cat();
		  s.setAge(11);
		  s.setName("TOM");
		  s.setList(new ArrayList<String>());
		  
		  
		  Person p=new Person();
		  p.setCat(s);
		  p.setI(44);
		  p.setS("FRGT");
		  Person pclone=  (Person) p.clone();
		  System.out.println(p==pclone);
		  
		  
		  
		  System.out.println(p.getCat()==pclone.getCat());
		  System.out.println(p.getCat().getName());
		  pclone.getCat().setName("gr");
		  System.out.println(p.getCat().getName());
		  
						  
		  
	}
	
	//序列化和反序列化属于深克隆
	@Test
	public void f() {
		Cat s=new Cat();
		  s.setAge(11);
		  s.setName("TOM");
		  s.setList(new ArrayList<String>());
		  
		  
		  Person p=new Person();
		  p.setCat(s);
		  p.setI(44);
		  p.setS("FRGT");
		  Person pclone=  (Person) p.deepClone();
		  System.out.println(p==pclone);
		  System.out.println(p.getCat()==pclone.getCat());
		  System.out.println(p.getCat().getName());
		  pclone.getCat().setName("gr");
		  System.out.println(p.getCat().getName());	
		  System.out.println(pclone.getCat().getName());	
		
	}
	
	
	//使用spring的工具类，属于浅复制
	@Test
	public void ff() {
		  Cat s=new Cat();
		  s.setAge(11);
		  s.setName("TOM");
		  s.setList(new ArrayList<String>());
		  
		  
		  Person p=new Person();
		  p.setCat(s);
		  p.setI(44);
		  p.setS("FRGT");
		  
		  Person pclone=new Person();
		  p.deepd(p, pclone);
		  System.out.println(p==pclone);
		  System.out.println(p.getCat()==pclone.getCat());
		  System.out.println(p.getCat().getName());
		  pclone.getCat().setName("gr");
		  System.out.println(p.getCat().getName());	 
		
	}

}
